package cc.telecomdigital.tdstock.activity.groups.monitor.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.DataRead;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.XMLHelper;

/**
 * 
 * Project：tdstock3
 * Package：cc.telecomdigital.tdstock.activity.groups.monitor.util
 * Class：ReadBookMark Description： Creater：hunter
 * [chengpanggang@126.com//hunter@mango.cc] CreateTime：Date：Nov 2, 2011
 * Time：2:53:11 PM Modifier ：hunter [chengpanggang@126.com//hunter@mango.cc]
 * Modify time： Date：Nov 2, 2011 Time：2:53:11 PM Modify Remarks： read Book Mark
 * 
 * @version
 * 
 */
public class ReadBookMark
{
	public static final String	TAG	= ReadBookMark.class.getSimpleName();

	private static ReadBookMark	rbm;

	public static ReadBookMark getInstance()
	{
		if (rbm == null)
		{
			rbm = new ReadBookMark();
		}
		return rbm;
	}

	private ReadBookMark()
	{
	}

	/*
	 * public Map<Integer, Record> getBookMarkRecord(String url, Context
	 * context) { Map<Integer, Record> maps = new HashMap<Integer, Record>();
	 * TDStockLog.d(TAG, url); Document doc = XMLHelper
	 * .parseURL(DataRead.getInputStreamFromURL(url)); if (doc == null) { return
	 * null; } Element root = doc.getDocumentElement(); NodeList lists =
	 * root.getElementsByTagName("Record"); if (lists.getLength() == 0) {
	 * 
	 * } for (int i = 0; i < lists.getLength(); i++) { NodeList record =
	 * lists.item(i).getChildNodes(); Record r = new Record(); for (int j = 0; j
	 * < record.getLength(); j++) { Element e = (Element) record.item(j); String
	 * value = e.getFirstChild().getNodeValue(); if
	 * (e.getNodeName().equalsIgnoreCase("Position".trim())) {
	 * r.setPosition(Integer.parseInt(value)); } else if
	 * (e.getNodeName().equalsIgnoreCase("Item".trim())) { r.setItem(value); }
	 * else if (e.getNodeName().equalsIgnoreCase("TypeId".trim())) {
	 * r.setType(Integer.parseInt(value)); } } maps.put(r.getPosition(), r); }
	 * return maps; }
	 */

	public Map<Integer, Record> getBookMarkRecord(String url, Context context)
	{
		Map<Integer, Record> maps = new HashMap<Integer, Record>();
		TDStockLog.d(TAG, url);
		Document doc = XMLHelper
				.parseURL(DataRead.getInputStreamFromURL(url));
		if (doc == null)
		{
			return null;
		}
		Element root = doc.getDocumentElement();
		NodeList lists = root.getElementsByTagName("Record");

		for (int i = 0; i < lists.getLength(); i++)
		{
			NodeList record = lists.item(i).getChildNodes();
			Record r = new Record();
			int position = -1;
			String item = null;
			int typeid = -1;
			try
			{
				for (int j = 0; j < record.getLength(); j++)
				{
					Element e = (Element) record.item(j);
					String value = e.getFirstChild().getNodeValue();
					if (e.getNodeName()
							.equalsIgnoreCase("Position".trim()))
					{
						position = Integer.parseInt(value);
					}
					else if (e.getNodeName().equalsIgnoreCase(
							"Item".trim()))
					{
						item = value;
					}
					else if (e.getNodeName().equalsIgnoreCase(
							"TypeId".trim()))
					{
						typeid = Integer.parseInt(value);
					}
				}
			}
			catch (Exception e)
			{
				continue;
			}
			if (position != -1 && null != item && typeid != -1)
			{
				r.setItem(item);
				r.setPosition(position);
				r.type = typeid;
				maps.put(r.getPosition(), r);
			}

		}
		return maps;
	}

	class Record
	{
		private String	item;
		private int	Position;
		private int	type;

		public String getItem()
		{
			return item;
		}

		public void setItem(String item)
		{
			this.item = item;
		}

		public int getPosition()
		{
			return Position;
		}

		public void setPosition(int position)
		{
			Position = position;
		}

		public int getType()
		{
			return type;
		}

		public void setType(int type)
		{
			this.type = type;
		}

		/**
		 * @param item
		 * @param position
		 * @param type
		 */
		public Record(String item, int position, int type)
		{
			super();
			this.item = item;
			Position = position;
			this.type = type;
		}

		/**
		 * 
		 */
		public Record()
		{
			super();
		}
	}

	// public static void main(String[] args) {
	// ReadBookMark rbm=new ReadBookMark();
	// Map<Integer,Record> m=rbm.getBookMarkRecord("");
	// for (Integer i : m.keySet()) {
	// System.out.println(i+"--->"+m.get(i).getItem());
	// }
	// }
	// /**
	// * get node tostring of nodetype
	// * @param node
	// * @return
	// */
	// public static String getNodeValue(Node node){
	// String str="";
	// NodeList temp=node.getChildNodes();
	// for (int i = 0; i < temp.getLength(); i++) {
	// if(temp.item(i).getNodeType()==Node.TEXT_NODE){
	// str=temp.item(i).toString().replace("#text:", "").replace("[",
	// "").replace("]", "").trim();
	// }
	// }
	// return str;
	// }
	// map convert to list
	public List<String[]> getItems(String url, Context context)
	{
		Map<Integer, Record> maps = rbm.getBookMarkRecord(url, context);
		if (maps == null)
		{
			return null;
		}
		TDStockLog.d(TAG, maps.size() + "");
		List<String[]> lists = new ArrayList<String[]>();
		for (int num : maps.keySet())
		{
			String[] temp = new String[2];
			temp[0] = maps.get(num).getItem();
			temp[1] = maps.get(num).getType() + "";
			lists.add(temp);
		}

		return lists;
	}
}
